﻿using UnityEditor;
using UnityEngine;
using System.Collections;
using System.IO;
using LitJson;

public class PUEXExporter : MonoBehaviour {

	[MenuItem("PUEX/Export")]
	public static void Export ()
	{
		DirectoryInfo resourceDirectory = null;
		DirectoryInfo exportDirectory = null;
		
		// export particle
		int puxCount = 0;
		resourceDirectory = new DirectoryInfo (Application.dataPath + "/Resource");
		exportDirectory = new DirectoryInfo (Path.GetDirectoryName (Application.dataPath) + "/export/particle");
		foreach (FileInfo fileInfo in resourceDirectory.GetFiles()) {
			if (Path.GetExtension(fileInfo.Name) == ".prefab") {
				var prefab = AssetDatabase.LoadAssetAtPath<GameObject> ("Assets/Resource/" + fileInfo.Name);
				if (prefab) {
					PUEXParticleSystem selectedSystem = prefab.GetComponent<PUEXParticleSystem>();
					if (selectedSystem) {
						JsonData data = new JsonData();
						selectedSystem.save (data);
						string json = data.ToJson();

						string exportFileName = Path.GetFileNameWithoutExtension(fileInfo.Name) + ".pux";
						string exportPath = exportDirectory + "/" + exportFileName;
						FileStream fs = null;
						StreamWriter sw = null;
						try {
							fs = new FileStream(exportPath, FileMode.Create);
							var utf8WithoutBom = new System.Text.UTF8Encoding(false);
							sw = new StreamWriter(fs, utf8WithoutBom);
							sw.Write(json);
							sw.Flush();
							Debug.Log ("export particle:" + exportPath);
							puxCount++;
						} catch (System.Exception ex) {
							Debug.LogException(ex);
						} finally {
							if (fs != null)
								fs.Close ();
							if (sw != null)
								sw.Close ();
						}
					}
				}
			}
		}

		// export mesh
		int meshCount = 0;
		resourceDirectory = new DirectoryInfo (Application.dataPath + "/Resource/mesh");
		exportDirectory = new DirectoryInfo (Path.GetDirectoryName (Application.dataPath) + "/export/mesh");
		foreach (FileInfo fileInfo in resourceDirectory.GetFiles()) {
			if (Path.GetExtension(fileInfo.Name).ToUpper() == ".FBX") {
				try {
					string exportPath = exportDirectory + "/" + fileInfo.Name;
					File.Copy(fileInfo.FullName, exportPath, true);
					Debug.Log ("copy mesh:" + exportPath);
					meshCount++;
				} catch (System.Exception ex) {
					Debug.LogException(ex);
				}
			}
		}
		
		// export texture
		int texCount = 0;
		resourceDirectory = new DirectoryInfo (Application.dataPath + "/Resource/texture");
		exportDirectory = new DirectoryInfo (Path.GetDirectoryName (Application.dataPath) + "/export/texture");
		foreach (FileInfo fileInfo in resourceDirectory.GetFiles()) {
			string fileExtUpper = Path.GetExtension(fileInfo.Name).ToUpper();
			if (fileExtUpper == ".PNG" || fileExtUpper == ".TGA" || fileExtUpper == ".JPG") {
				try {
					string exportPath = exportDirectory + "/" + fileInfo.Name;
					File.Copy(fileInfo.FullName, exportPath, true);
					Debug.Log ("copy texture:" + exportPath);
					texCount++;
				} catch (System.Exception ex) {
					Debug.LogException(ex);
				}
			}
		}

		// export sound
		int sndCount = 0;
		resourceDirectory = new DirectoryInfo (Application.dataPath + "/Resource/sound");
		exportDirectory = new DirectoryInfo (Path.GetDirectoryName (Application.dataPath) + "/export/sound");
		foreach (FileInfo fileInfo in resourceDirectory.GetFiles()) {
			string fileExtUpper = Path.GetExtension(fileInfo.Name).ToUpper();
			if (fileExtUpper == ".WAV") {
				try {
					string exportPath = exportDirectory + "/" + fileInfo.Name;
					File.Copy (fileInfo.FullName, exportPath, true);
					Debug.Log ("copy sound:" + exportPath);
					sndCount++;
				} catch (System.Exception ex) {
					Debug.LogException(ex);
				}
			}
		}

		Debug.Log ("export finish pux:" + puxCount + " mesh:" + meshCount + " texture:" + texCount + " sound:" + sndCount);
	}

}
